using UnityEngine;
using System.Collections;

/// \class  gkSpawningLinearVelocityModifier
/// \brief  Select a linear velocity
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawningLinearVelocityModifier")]
public class gkSpawningLinearVelocityModifier : gkSpawningModifier 
{
	/// The velocity direction
	public Vector3 VelocityMin = new Vector3(0,0,0);

	/// The velocity direction
	public Vector3 VelocityMax = new Vector3(0,0,0);
	
	/// Modify the X velocity ?
	public bool ModifyX = true;
	
	/// Modify the X velocity ?
	public bool ModifyY = true;
	
	/// Modify the X velocity ?
	public bool ModifyZ = true;
	
	/// scale the velocity ?
	public bool Scale;
	
	/// \brief  Event called at the and of the actor spawn
	public override void ModifySpawnedActor(GameObject a_rSpawnedActor, int a_iNumActorsInCurrentBatch, int a_iNbActorsInCurrentBatch)
	{
		RandomVelocityChange(a_rSpawnedActor);
	}
	
	/// \brief  Random velocity change
	void RandomVelocityChange(GameObject a_rSpawnedActor)
	{
		Vector3 f3RandomVelocity;
		gkPhysicComponent rMainPhysicComponentSpawnerActor;
		
		// Grab the physic main component
		rMainPhysicComponentSpawnerActor = a_rSpawnedActor.GetComponent<gkPhysicComponent>();
		
		// Save the current velocity
		f3RandomVelocity = rMainPhysicComponentSpawnerActor.GetLinearVelocity();
		
		// Have we to scale or override the velocity
		if(Scale)
		{
			// Scale X velocity if needed
			if(ModifyX)
			{
				f3RandomVelocity.x *= Random.Range(VelocityMin.x, VelocityMax.x);				
			}
			
			// Scale Y velocity if needed
			if(ModifyY)
			{
				f3RandomVelocity.y *= Random.Range(VelocityMin.y, VelocityMax.y);				
			}
			
			// Scale Z velocity if needed
			if(ModifyZ)
			{
				f3RandomVelocity.z *= Random.Range(VelocityMin.z, VelocityMax.z);				
			}
		}
		else
		{	
			// Override X velocity if needed
			if(ModifyX)
			{
				f3RandomVelocity.x = Random.Range(VelocityMin.x, VelocityMax.x);				
			}
			
			// Override Y velocity if needed
			if(ModifyY)
			{
				f3RandomVelocity.y = Random.Range(VelocityMin.y, VelocityMax.y);				
			}
			
			// Override Z velocity if needed
			if(ModifyZ)
			{
				f3RandomVelocity.z = Random.Range(VelocityMin.z, VelocityMax.z);				
			}
		}
		
		// Set the velocity
		rMainPhysicComponentSpawnerActor.SetLinearVelocity(f3RandomVelocity);
	}
}
